/**   
* @Title: ScmInvocingSupplyServiceImpl.java 
* @Package com.kerrykidz.scminvoicing.service.impl 
* @Description: 用一句话描述该文件做什么 
* Copyright: Copyright (c) 2014 
* Company:wuwh team by iss
* @author: ScreenJxp   
* @date: 2014年8月13日 下午2:01:02 
* @version: V1.0
* update Release(文件修正记录)
* <pre>
* author--updateDate--description----------------------Flag————
* wuwh    2014-5-1    测试codesyle                      #wuwh001
*
*
*
* </pre>
*
*/
package com.kerrykidz.scminvoicing.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.common.dao.mapper.SysAttFileEntityMapper;
import com.kerrykidz.dao.model.SysAttFileEntity;
import com.kerrykidz.scminvoicing.dao.mapper.ScmSupAgmEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmSupEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmSupPrdEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.ScmSupAgmEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmSupEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmSupPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingSupplyService;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;
/**
 * 
* @ClassName: ScmInvocingSupplyServiceImpl 
* @description:供应商操作的service接口
* @author: ScreenJxp 
* @date: 2014年8月21日 下午1:29:06 
* @version: V1.0
*
 */
@Service
public class ScmInvocingSupplyServiceImpl implements IScmInvocingSupplyService {
	@Autowired
	private ScmSupEntityMapper scmSupEntityMapper;
	/**供应商协议*/
	@Autowired
	private ScmSupAgmEntityMapper scmSupAgmEntityMapper;
	/**与供应商签订协议包含的商品*/
	@Autowired
	private ScmSupPrdEntityMapper scmSupPrdEntityMapper;
	/**文件路径关联*/
	@Autowired
	private SysAttFileEntityMapper sysAttFileEntityMapper;
	
	public List<ScmSupEntity> findSupplyInfo(ScmSupEntity entity)
			throws RDPException {
			List<ScmSupEntity> resultList=scmSupEntityMapper.findSupplyInfo(entity);
			return resultList;
	}
	public void saveSupply(ScmSupEntity entity) throws RDPException {
			scmSupEntityMapper.saveSupply(entity);
	}

	public void deleteSupply(ScmSupEntity entity) throws RDPException {
			scmSupEntityMapper.deleteSupply(entity);
	}
	public void updateSupply(ScmSupEntity entity) throws RDPException {
			scmSupEntityMapper.updateSupply(entity);
	}
	public void saveSupAgm(ScmSupAgmEntity entity) throws RDPException {
		//关联供应商协议附件信息
		SysAttFileEntity attFileEntity=new SysAttFileEntity();
		String pkAttfileId=GenerationUUID.getGenerationUUID();
		String picFile=entity.getFkAgmFile();
		String filePath=picFile;
		String fileName=picFile.substring(picFile.lastIndexOf("\\")+1);
		attFileEntity.setPkAttfileId(pkAttfileId);
		attFileEntity.setAttfileName(fileName);
		attFileEntity.setAttfilePath(filePath);
		attFileEntity.setAttType("7");
		attFileEntity.setDelFlag("0");
		attFileEntity.setModiBy(entity.getModiBy());
		attFileEntity.setFkBranId(entity.getFkBranId());
		sysAttFileEntityMapper.insertAttFile(attFileEntity);
		entity.setFkAgmFile(pkAttfileId);//设置外键关联
		scmSupAgmEntityMapper.saveEntity(entity);
		List<String> result=entity.getIdAndPrice();
		if(result!=null&&result.size()>0){
				ScmSupPrdEntity prd=new ScmSupPrdEntity();
				prd.setFkSupagmId(entity.getPkSupagmId());
				String[] arr;
				for(String str:result){
					arr=str.split("_");
					prd.setAgmPrice(Double.parseDouble(arr[1]));
					prd.setFkPrdId(arr[0]);
					prd.setFkSupagmId(entity.getPkSupagmId());
					scmSupPrdEntityMapper.saveEntity(prd);
				}
		}
	}
	public List<ScmSupAgmEntity> getSupAgmInfo(ScmSupAgmEntity entity)
			throws RDPException {
			List<ScmSupAgmEntity> resultList=scmSupAgmEntityMapper.getSupAgmInfo(entity);
			return resultList;
	}
	public List<ScmSupPrdEntity> getAgmProductInfo(String fkSupagmId)
			throws RDPException {
			List<ScmSupPrdEntity> resultList=scmSupPrdEntityMapper.getAgmProductInfo(fkSupagmId);
			return resultList;
	}
	public String deleteBefore(String fkSupId) throws RDPException {
		List<ScmSupEntity> resultList=scmSupEntityMapper.deleteBefore(fkSupId);
		ScmSupEntity entity=resultList!=null&&resultList.size()>0?resultList.get(0):null;
		if(entity==null){
			return "ok";
		}
		int passed=entity.getPassed();
		if(passed>0){
			return "passed";
		}else{
			return "passing";
		}
	}
	public void  deleteSupAgm(ScmSupAgmEntity entity) throws RDPException {
			scmSupAgmEntityMapper.deleteSupAgm(entity);
	}
	public void deleteSupPrd(ScmSupPrdEntity entity) throws RDPException {
		   scmSupPrdEntityMapper.deleteSupPrd(entity);
	}
	public void updatePrice(List<String> prdInfo,String fkSupagmId) throws RDPException {
			if(prdInfo!=null &&prdInfo.size()>0){
				for (String arr:prdInfo) {
						String [] str=arr.split("_");
						ScmSupPrdEntity entity=new ScmSupPrdEntity();
						entity.setFkSupagmId(fkSupagmId);
						entity.setFkPrdId(str[0]);
						entity.setAgmPrice(Double.parseDouble(str[3]));
						scmSupPrdEntityMapper.updatePrice(entity);
				}
			}
	}
	public void savePrdPrice(List<String> prdInfo,String fkSupagmId) throws RDPException {
		if(prdInfo!=null &&prdInfo.size()>0){
			for (String arr:prdInfo) {
					String [] str=arr.split("_");
					ScmSupPrdEntity entity=new ScmSupPrdEntity();
					entity.setFkSupagmId(fkSupagmId);
					entity.setFkPrdId(str[0]);
					entity.setAgmPrice(Double.parseDouble(str[3]));
					scmSupPrdEntityMapper.saveEntity(entity);
			}
		}
	}
	public void updateSupAgm(ScmSupAgmEntity entity) throws RDPException {
			scmSupAgmEntityMapper.updateSupAgm(entity);
	}
	public String isDelete(String fkAgmId, String fkPrdId) throws RDPException {
		List<String> list=scmSupPrdEntityMapper.deleteAgmOrProductBefor(fkAgmId, fkPrdId);
		int unComplet=0;
		int Completed=0;
		if(list==null ||list.get(0)==null){
			return "0";//可以删除
		}else{
			String[] str;
			for(String arr:list){
				str=arr.split("_");
				if("2".equals(str[0])){
					unComplet= Integer.parseInt(str[1]);
				}
				if("3".equals(str[0])){
					Completed= Integer.parseInt(str[1]);
				}
			}
			if(Completed>0){
				return "1";//不可删除
			}
			if(unComplet>0){
				return "2";//删除给出提示
			}
		}
		return "0";
		
	}
}
